package com.wst.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class PropertiesReader {

	private static Map<String,Properties> mp = new HashMap<String,Properties> ();
	private static Object lock = new Object();

	public static String getProperties(String key) {
		return getProperties("config.properties", key);
	}

	/**
	 * 读取指定配置文件
	 * @param file
	 * @param key
	 * @return
	 */
	public static String getProperties(String file, String key) {
		if (mp != null && !mp.isEmpty() && mp.containsKey(file)) {
			return mp.get(file).getProperty(key);
		}
		InputStream input = null;
		String message = null;
		try {
			synchronized (lock) {
				if (mp != null && mp.get(file) != null) {
					return mp.get(file).getProperty(key);
				}
				input = PropertiesReader.class.getClassLoader().getResourceAsStream(file);
				Properties tmp = new Properties();
				tmp.load(input);
				mp.put(file, tmp);
				message = mp.get(file).getProperty(key);
			}

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (input != null)
				try {
					input.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
		return message;
	}
}
